package com.mycompany.oj.controller;

import com.mycompany.oj.aspect.JudgeLog;
import com.mycompany.oj.entity.JsonResult;
import com.mycompany.oj.entity.ResultCode;
import com.mycompany.oj.entity.studentInformation;
import com.mycompany.oj.service.Impl.StudentServiceImpl;
import com.mycompany.oj.util.SharedService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author asus
 */
@RestController
@Api(tags = "StudentController", description = "学生信息接口")
public class StudentContoller {

    @Autowired
    StudentServiceImpl studentservice;


    private SharedService sharedService = new SharedService();
    //获取个人信息
    @JudgeLog
    @RequestMapping("/get_Student_Information")
    public JsonResult getStudentInformation(HttpServletRequest request) throws Exception {
        String username = sharedService.getUsername(request);
        return new JsonResult(ResultCode.SUCCESS,studentservice.getStudentInformationService(username));
    }

    //修改手机号
    @JudgeLog
    @RequestMapping("/change_phone")
    public JsonResult doChangePhone(@RequestParam("newnumber") String newnumber, HttpServletRequest request) throws Exception {
        String username =  sharedService.getUsername(request);
        return studentservice.changePhoneService(newnumber, username);
    }

    //修改密码
    @JudgeLog
    @RequestMapping("/change_password")
    public JsonResult doChangePassword(HttpServletRequest request) throws Exception {
        String username =  sharedService.getUsername(request);
        String newpassword = request.getParameter("password");
        return studentservice.changePasswordService(newpassword, username);
    }

    //个人页面左边信息展示
    @JudgeLog
    @RequestMapping("/left_Information")
    public JsonResult doShowLeftInformation(HttpServletRequest request) throws Exception {
        String username =  sharedService.getUsername(request);
        return new JsonResult(ResultCode.SUCCESS,studentservice.showInformationService(username));
    }
    
    //从管理进去的学生界面
    @RequestMapping("/left_Information_t")
    public JsonResult doShowLeftInformationT(@RequestParam("schoolid") String schoolid) {
        return new JsonResult(ResultCode.SUCCESS,studentservice.showInformationServiceT(schoolid));
    }

    //修改计划
    @JudgeLog
    @RequestMapping("/change_plan")
    public JsonResult doChangePlan(HttpServletRequest request, HttpSession session) throws Exception {
        String number = (String) (request.getParameter("newnumber"));

        return studentservice.changePlanService(number,  sharedService.getUsername(request));
    }

    //概况
    @JudgeLog
    @RequestMapping("/summarize")
    public JsonResult doSummarize(HttpServletRequest request) throws Exception {
        String username =  sharedService.getUsername(request);
        return new JsonResult(ResultCode.SUCCESS,studentservice.summarizeService(username));
    }
    @RequestMapping("/summarize_t")
    public JsonResult doSummarizeT(@RequestParam("schoolid") String schoolid) {

        return new JsonResult(ResultCode.SUCCESS,studentservice.summarizeTService(schoolid));
    }

    //收藏夹
    @JudgeLog
    @RequestMapping("/get_favorites")
    public JsonResult doGetFavorites(HttpServletRequest request) throws Exception {
        return new JsonResult(ResultCode.SUCCESS,studentservice.getFavoritesService( sharedService.getUsername(request)));
    }

    //删除收藏的题目
    @JudgeLog
    @RequestMapping("/delete_question")
    public JsonResult doDeleteQuestion(HttpServletRequest request) throws Exception {
        int question_id = Integer.parseInt(request.getParameter("question_id"));

        return studentservice.deleteQuestionService( sharedService.getUsername(request), question_id);
    }
    
    //获取各个难度num
    @RequestMapping("/question_difficult_num")
    public JsonResult doQuestionDifficultNum() {
        return new JsonResult(ResultCode.SUCCESS,studentservice.getDifficultNum());
    }
    
    //获取最近五道题的动态
    @JudgeLog
    @RequestMapping("/five_student")
    public JsonResult getFiveRegister(HttpServletRequest request) throws Exception {
        String username =  sharedService.getUsername(request);
        return new JsonResult(ResultCode.SUCCESS,studentservice.getFiveStudent(username));
    }
    
    @RequestMapping("/watch_student")
    public JsonResult getShitRegister(@RequestParam("schoolid") String schoolid){
        return new JsonResult(ResultCode.SUCCESS,studentservice.getFiveStudentSchoolId(schoolid));
    }
    
    //修改学生信息
    @JudgeLog
    @RequestMapping("/change_student_Information")
    public JsonResult doRegisterInformation(HttpServletRequest request, HttpSession session) throws Exception {
        String nickname = request.getParameter("nickname");
        String sex = request.getParameter("sex");
        String school = request.getParameter("school");
        String grade = request.getParameter("grade");
        String major = request.getParameter("major");
        String schoolid = request.getParameter("schoolid");
         String realname = request.getParameter("realname");

        studentInformation student_information = new studentInformation();
        student_information.setNickname(nickname);
        student_information.setSex(sex);
        student_information.setSchool(school);
        student_information.setGrade(grade);
        student_information.setMajor(major);
        student_information.setSchoolid(schoolid);
        student_information.setStudent_name(realname);

        return studentservice.changePersonalInformation(student_information,  sharedService.getUsername(request));
    }
    
    //判题入口
    @JudgeLog
    @RequestMapping("/gogogo")
    public JsonResult doGogogo(@RequestParam("file") String file, @RequestParam("language") String language, @RequestParam("question_id") String question_id , @RequestParam("status") String status, HttpServletRequest request) throws Exception {
        String username =  sharedService.getUsername(request);
        return studentservice.practice(file, language, question_id,username,status);
    }
    

    
    //测试数据
    @RequestMapping("/result")
    public JsonResult doResult(){
        return studentservice.doResult();
    }
    
    //上传答案
    @RequestMapping("/post_answer")
    public JsonResult doPostAnswer(@RequestParam("language") String language, HttpServletRequest request, @RequestParam("answer") String answer, @RequestParam("question_id") String question_id) throws Exception {
        String username = sharedService.getUsername(request);
        return studentservice.doPostAnswer(username, question_id, answer, language);
    } 
}
